/*
 * RVector.java -
 * Copyright (c) 2005 Carmine Lia
 * e-mail:      carmine.lia@libero.it
 * web-address: http://www.intermedia.sa.it/lia

 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
package javax.robotics.vecmath;

import java.io.PrintStream;
import java.util.Locale;

import javax.vecmath.GVector;
import javax.vecmath.MismatchedSizeException;

/**
 * A double precision, general, dynamically-resizable, one-dimensional vector
 * class. Index numbering begins with zero. This class extend printing
 * functionality of GVector class.
 * 
 * @author <a href="http://www.intermedia.sa.it/lia/">Carmine Lia</a>
 * @since 1.0.1
 * @version 5/10/2005
 */
public class RVector extends GVector
{
    private static final String strLineSeparator = "line.separator";
    private static final String newLine = System.getProperty(strLineSeparator);
    private static final String formatString = "%9.4f" + newLine;
    /**
     * 
     */
    private static final long serialVersionUID = 3978428001035498549L;

    /**
     * Constructs a new RVector from the specified RVector. The vector elements
     * are copied into this new RVector.
     * 
     * @param vector
     *            The source RVector for this new RVector.
     */
    public RVector(RVector vector)
    {
        super(vector);
    }
    /**
     * Constructs a new RVector from the specified RVector3d. The vector elements
     * are copied into this new RVector.
     * 
     * @param vector
     *            The source RVector3d for this new RVector.
     */
    public RVector(RVector3d vector)
    {
        super(vector);
    }
    /**
     * Constructs a new RVector from the specified GVector. The vector elements
     * are copied into this new RVector.
     * 
     * @param vector
     *            The source GVector for this new RVector.
     */
    public RVector(GVector vector)
    {
        super(vector);
    }

    /**
     * Constructs a new RVector from the specified array elements. The length of
     * this RVector is set to the length of the specified array. The array
     * elements are copied into this new RVector.
     * 
     * @param vector
     *            The values for the new RVector.
     */
    public RVector(double... vector)
    {
        super(vector);
    }

    /**
     * Constructs a new RVector of the specified length and initializes it by
     * copying the specified number of elements from the specified array. The
     * array must contain at least length elements (i.e., vector.length >=
     * length. The length of this new RVector is set to the specified length.
     * 
     * @param vector
     *            The array from which the values will be copied.
     * @param length
     *            The number of elements of array to be copied in the new
     *            RVector.
     */
    public RVector(double[] vector, int length)
    {
        super(vector, length);
    }

    /**
     * Constructs a new RVector of the specified length with all vector elements
     * initialized to 0.
     * 
     * @param length
     *            The number of elements in this RVector.
     */
    public RVector(int length)
    {
        super(length);
    }

    /**
     * Prints to standard out
     */
    public void printf()
    {
        this.printf(System.out, formatString);
    }

    /**
     * Prints to standard out according to the format string.
     * <p>
     * The format specifiers for numeric types have the following syntax:
     * 
     * <pre>
     *  %[width][.precision]conversion
     * </pre>
     * 
     * <ul>
     * <li> The optional <b><code>width</code></b> is a non-negative decimal
     * integer indicating the minimum number of characters to be written to the
     * output. </li>
     * <li> The optional <b><code>precision</code></b> is a non-negative
     * decimal integer usually used to restrict the number of characters. The
     * specific behavior depends on the conversion. </li>
     * <li> The required <b><code>conversion</code></b> is a character
     * indicating how the argument should be formatted. The set of valid
     * conversions for a given argument depends on the argument's data type.
     * </li>
     * </ul>
     * <p>
     * Example: </br><code>v.printf("%5.3f");</code>
     * </p>
     * 
     * @param format
     *            The format string
     * @see PrintStream#format(java.lang.String, java.lang.Object[])
     */
    public void printf(String format)
    {
        this.printf(System.out, format);
    }

    /**
     * Prints to <code>PrintStream</code>
     * 
     * @param out
     *            The <code>PrintStream</code> object.
     */
    public void printf(PrintStream out)
    {
        this.printf(out, formatString);
    }

    /**
     * Prints to <code>PrintStream</code> according to the format string
     * 
     * @param out
     *            The <code>PrintStream</code> object.
     * @param format
     *            The format string.
     * @see #printf(String)
     */
    public void printf(PrintStream out, String format)
    {
        final int n = this.getSize();
        for (int i = 0; i < n; i++)
        {
            out.printf(Locale.US, format, this.getElement(i));
        }
    }


    /**
     * Returns a string that contains the values of this RVector.
     * 
     * @return The string.
     */
    public String toString()
    {
         return this.toString(formatString);
    }

    /**
     * Returns a string that contains the values of this RVector, according to
     * the format string
     * 
     * @param format
     *            The format string
     * @return The string
     * @see #printf(String)
     */
    public String toString(String format)
    {

        final int n = this.getSize();

        final StringBuffer buffer = new StringBuffer(n * 8);

        for (int i = 0; i < n; i++)
        {
            buffer.append(String.format(Locale.US, format, this.getElement(i)));
        }

        return buffer.toString();

    }

    /**
     * Gets this vector as array
     * 
     * @param vector
     *            the array
     */
    public void get(double[] vector)
    {
        final int n = getSize();
        for (int i = 0; i < n; i++)
            vector[i] = getElement(i);

    }

    /**
     * Appends the input vector to this vector.
     * 
     * @param v
     *            the input vector.
     */
    public void append(RVector v)
    {
        int k = 0;
        final int oldSize = this.getSize();
        final int newSize = oldSize + v.getSize();
        this.setSize(newSize);
        for (int i = oldSize; i < newSize; i++)
        {
            this.setElement(i, v.getElement(k));
            k++;
        }

    }

    /**
     * Appends the input array to this vector.
     * 
     * @param v
     *            the input vector.
     */
    public void append(double[] v)
    {
        int k = 0;
        final int oldSize = this.getSize();
        final int newSize = oldSize + v.length;
        this.setSize(newSize);
        for (int i = oldSize; i < newSize; i++)
        {
            this.setElement(i, v[k]);
            k++;
        }

    }

    /**
     * Appends the input vector to this vector.
     * 
     * @param v
     *            the input vector.
     */
    public void append(RVector3d v)
    {
        final int oldSize = this.getSize();
        this.setSize(oldSize + 3);
        this.setElement(oldSize, v.x);
        this.setElement(oldSize + 1, v.y);
        this.setElement(oldSize + 2, v.z);

    }
    /**
     * Splits this vector in two new vectors. The size of 1st vector is
     * size1=round(this.size/2), the size of 2nd one is size2=this.size-size1.
     * 
     * @param v1
     *            the first vector to be copied.
     * @param v2
     *            the second vector to be copied.
     */
    public void split(RVector v1, RVector v2)
    {
        int k = 0;

        final int size = this.getSize();
        final int size1 = Math.round(size / 2);
        final int size2 = size - size1;
        v1.setSize(size1);
        v2.setSize(size2);
        for (int i = 0; i < size1; i++)
            v1.setElement(i, this.getElement(i));
        for (int i = size1; i < size; i++)
        {
            v2.setElement(k, this.getElement(i));
            k++;
        }
    }

    /**
     * Splits this vector in three new vectors. The size of 1st end 2nd vectors
     * is size=round(this.size/3), the size of 3rd one is
     * size3=this.size-2*size.
     * 
     * @param v1
     *            the first vector to be copied.
     * @param v2
     *            the second vector to be copied.
     * @param v3
     *            the second vector to be copied.
     */
    public void split(RVector v1, RVector v2, RVector v3)
    {
        int k = 0;

        final int size = this.getSize();
        final int size1 = Math.round(size / 3);
        final int size2 = size1;
        final int size3 = size - 2 * size1;
        v1.setSize(size1);
        v2.setSize(size2);
        v3.setSize(size3);
        for (int i = 0; i < size1; i++)
            v1.setElement(i, this.getElement(i));
        for (int i = size1; i < size1 + size2; i++)
        {
            v2.setElement(k, this.getElement(i));
            k++;
        }
        k = 0;
        for (int i = size2 + size1; i < size; i++)
        {
            v3.setElement(k, this.getElement(i));
            k++;
        }
    }
    /**
     * Adds multiple vectors and puts the result in this vector.
     * 
     * @param v
     *            the input vectors separate by comma
     */
    public void add(RVector... v)
    {
        double sum = 0.0;
        final int size = this.getSize();
        for (int j = 0; j < size; j++)
        {
            sum = 0.0;
            for (int i = 0; i < v.length; i++)
                sum += v[i].getElement(j);
            this.setElement(j, sum);
        }
    }
    /**
     * Adds multiple scaled vectors and puts the result in this vector.
     * 
     * @param s
     *            the array of scale factors.
     * @param v
     *            the input vectors separate by comma.
     */
    public void addScale(double[] s, RVector... v)
    {
        double sum = 0.0;
        final int size = this.getSize();

        if(s.length < v.length)
            throw new MismatchedSizeException(
                    "The sizes of input vectors are not agree");
        for (int j = 0; j < size; j++)
        {
            sum = 0.0;
            for (int i = 0; i < v.length; i++)
                sum += s[i] * v[i].getElement(j);
            this.setElement(j, sum);
        }
    }

    /**
     * Multiplies diagonal matrix D times RVector v and places the result into
     * this vector (this = D*v).
     * 
     * @param D
     *            The diagonal matrix in the multiplication
     * @param v
     *            The vector that is multiplied
     */
    public void mul(DMatrix D, RVector v)
    {
        final int n = D.getSize();
        if(n != v.getSize())
            throw new MismatchedSizeException(
                    "The the dimensions of diagonal matrix and of the vector are not agree");
        for (int i = 0; i < n; i++)
            this.setElement(i, D.getElement(i) * v.getElement(i));
    }

    /**
     * Sets this vector from varargs input.
     * 
     * @param vector
     *            the elements of vector separated by comma.
     */
    public void setValues(double... vector)
    {
        this.set(vector);
    }
}